from common import exception
from common.LoggerFactory import Log
from config import configuration
from service_sms.sdk.dysmsapi import AlibabaSmsSDK


class AlibabaSmsApi:

    @staticmethod
    def send_sms(context, template_code, params):
        """
        发送短息
        :param context: 上下文信息
        :param template_code: 短信模板
        :param params: 模板参数
        """
        Log.console().info("正在发送短信...")
        try:
            endpoint = context.get(configuration.SMS_ENDPOINT_ATT)
            as_api = AlibabaSmsSDK(endpoint)
            as_api.send_sms(context, template_code, params)
        except exception.CheckedException as ce:
            Log.error(f"错误：{ce.message}")
            raise exception.ServiceException(ce.message)
        Log.console().info("短信已发送")

    @staticmethod
    def send_batch_sms(context, template_code, params):
        """
        批发短信
        :param context: 上下文信息
        :param template_code: 短信模板
        :param params: 模板参数
        """
        Log.console().info("正在批发短信...")
        try:
            endpoint = context.get(configuration.SMS_ENDPOINT_ATT)
            as_api = AlibabaSmsSDK(endpoint)
            as_api.send_batch_sms(context, template_code, params)
        except exception.CheckedException as ce:
            Log.error(f"错误：{ce.message}")
            raise exception.ServiceException(ce.message)
        Log.console().info("短信已发送")
